﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

using Zuehlke.Blueprint.Quickstart.Architecture.Presentation.UserInterface.Localization;

namespace Zuehlke.Blueprint.Quickstart.Architecture.Presentation.UserInterface.ValueConverters
{
    /// <summary>
    /// Extracts the name of the class type and inserts spaces before each capital letter.
    /// </summary>
    public class ViewModelToNameValueConverter : ValueConverterMarkupExtensionBase, IMultiValueConverter
    {
        #region IMultiValueConverter Members

        /// <summary>
        /// See <see cref="IMultiValueConverter.Convert"/>.
        /// </summary>
        public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.Length < 2)
            {
                return DependencyProperty.UnsetValue;
            }
            
            var valueType = value[0].GetType();
            var typeName = valueType.Name;

            var strippedTypeName = typeName.Replace("ViewModel", string.Empty);
            return TranslationManager.Translate(string.Concat("Application.", strippedTypeName), value[1] as CultureInfo);
        }

        /// <summary>
        /// See <see cref="IMultiValueConverter.ConvertBack"/>.
        /// </summary>
        public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
